package com.hopes.bizz.risk.transfer.validation;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author lyb
 * @version 1.0
 * @description: 数据字典校验器
 * @date 2024/2/1 14:14
 */
public class IncludeValidator implements ConstraintValidator<Include, String> {

    private String[] item;

    private boolean allowEmpty;

    @Override
    public void initialize(Include include) {
        this.item = include.item();
        this.allowEmpty = include.allowEmpty();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)) {
            return allowEmpty;
        }

        if (item == null || item.length == 0) {
            return true;
        }
        return ArrayUtils.contains(item, value);
    }
}
